package org.testis.testdriver;

import org.testis.testdriver.parser.TestPlanParsingContext;
import org.testis.testdriver.exceptions.RequiredInformationMissing;
import org.testis.data.parser.DynamicMapTreeProviderFactory;
import java.util.Vector;
import org.orm.PersistentException;
import org.testis.orm.refinedclasses.DynamicMapDesignTreeNode;
import org.testis.orm.refinedclasses.DynamicMapParameter;
import org.testis.orm.refinedclasses.TestDesignTreeNode;
import org.testis.utils.middleware.TestDesign;


public class TestPlanParser
{

    private ITestBuilder builder;
    private TestDesignTreeNode currentNode;


    /**
     * This method is used to traverse the tree.
     */
    private void traverseTree() throws PersistentException
    {
        buildTestPart();
        TestDesignTreeNode[] nodesArray = currentNode.getChildrenForRun();
        for (int u = 0; u < nodesArray.length; u++) {
            currentNode = nodesArray[u];
            traverseTree();
        }
    }


    /**
     * This method is the parser of the Builder Design Pattern.
     */
    private void buildTestPart() throws PersistentException
    {
        
        TestPlanParsingContext newContext  = new TestPlanParsingContext(builder,
                                       currentNode);
        newContext.parseCurrentNode();

    }


    /**
     * This method returns the root TestPart of the constructed Composite Tree.
     * 
     * @param testSuiteName the name of the Test Suite to run
     * @return TestPart root of the Composite Tree
     */
   public TestPart construct(String testSuiteName)
    {
            TestDesign testDesign = TestDesign.newInstance();

            currentNode =
                testDesign.getTestDesignTreeByName(true,
                                                   testSuiteName);

            return construct(currentNode);


    }


    /**
     * This method returns the root TestPart of the constructed Composite Tree.
     * @param rootNode the root node of the TestDesignTree
     * @return TestPart root of the Composite Tree
     */
    public TestPart construct(TestDesignTreeNode rootNode)
    {
        try {
            builder = new TestPlanBuilder();

            currentNode = rootNode;

            traverseTree();

        } catch (PersistentException ex) {
            ex.printStackTrace();
        }
        catch (RuntimeException ex)
        {
            ex.printStackTrace();
        }
        
        checkForExceptions();

        return builder.getResult();



    }


    private String getFactoryIpFromDeviceId(String deviceId) throws
        PersistentException
    {

        DynamicMapDesignTreeNode rootNode =
            DynamicMapTreeProviderFactory.getInstance().
            getDynamicMapTree(deviceId);
        DynamicMapParameter factorySettingsIP = rootNode.getObject().
            getParameterByName("FactorySettingsIp");
        return factorySettingsIP.getReference().getValue();

    }
    
    private void checkForExceptions()
    {
        Vector<String> exceptions = ((TestPlanBuilder)builder).getExceptions();
        if(!exceptions.isEmpty())
        {
            StringBuilder strToBuild = new StringBuilder();
            for(int y= 0; y < exceptions.size(); y++)
            {
                strToBuild.append(exceptions.get(y));
                strToBuild.append('\n');
            }
            
            throw new RequiredInformationMissing(strToBuild.toString());
        }
    }
}

